﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DreamIn.Structures.ServerDatabase.Entities;
using MySql.Data.MySqlClient;

namespace DreamIn.Structures.ServerDatabase.DataAccessLayer
{
    class PictureDAL
    {
         public List<PictureEntity> GetPictures()
        {
            MySqlConnection connection = DALHelper.Connection;
            try
            {
               MySqlCommand command = connection.CreateCommand();
               MySqlDataReader Reader;

               command.CommandText = "select id_picture,path,id_album from pictures";
               connection.Open();
               Reader = command.ExecuteReader();
               List<PictureEntity> result=new List<PictureEntity>();
               while (Reader.Read()) {
                   result.Add(new PictureEntity() { id_picture = Reader.GetInt16(0), path=Reader.GetString(1), id_album=Reader.GetInt16(2)});
               }
               Reader.Close();
               return result;
            }
            finally
            {
                connection.Close();
            }
        }

         public void AddPicture(String path, int id_album)
         {
             MySqlConnection connection = DALHelper.Connection;
             try
             {
                 MySqlCommand command = connection.CreateCommand();

                 connection.Open();
                 command.CommandText = "INSERT INTO pictures (path,id_album) VALUES" + "('" + path + "','" + id_album + "')";
                 command.ExecuteNonQuery();
             }
             finally
             {
                 connection.Close();
             }
         }

         public void DeletePicture(PictureEntity picture)
         {
             MySqlConnection connection = DALHelper.Connection;
             try
             {
                 MySqlCommand command = connection.CreateCommand();

                 connection.Open();
                 command.CommandText = "DELETE FROM pictures WHERE id_picture=" + picture.id_picture;
                 command.ExecuteNonQuery();
             }
             finally
             {
                 connection.Close();
             }
         }

         public void UpdatePicture(int id_picture, String path, int id_album)
         {
             MySqlConnection connection = DALHelper.Connection;
             try
             {
                 MySqlCommand command = connection.CreateCommand();

                 connection.Open();
                 command.CommandText = "UPDATE pictures SET path='" + path+ "', id_album='" + id_album + "' WHERE id_picture=" + id_picture;
                 command.ExecuteNonQuery();
             }
             finally
             {
                 connection.Close();
             }
         }
    }
}
